package com.sky.controller.admin;

import com.sky.constant.JwtClaimsConstant;
import com.sky.dataTransmit.EmployeeDTO;
import com.sky.dataTransmit.EmployeeLoginDTO;
import com.sky.dataTransmit.PasswordEditDTO;
import com.sky.entity.Employee;
import com.sky.properties.JwtProperties;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.EmployeeService;
import com.sky.utils.JwtUtil;
import com.sky.view.EmployeeLoginVO;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/admin/employee")
@Slf4j
@Api(tags = "员工接口")
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;
    @Autowired
    private JwtProperties jwtProperties;

    @PostMapping("/login")
    public Result<EmployeeLoginVO> login(@RequestBody EmployeeLoginDTO employeeLoginDTO) {
        log.info("员工登录账号");
        Employee employee = employeeService.login(employeeLoginDTO);
        Map<String, Object> claims = new HashMap<>();
        claims.put(JwtClaimsConstant.EMP_ID, employee.getId());
        String token = JwtUtil.createJWT(jwtProperties.getAdminSignKey(), jwtProperties.getAdminTtl(), claims);
        EmployeeLoginVO employeeLoginVO = EmployeeLoginVO.builder()
                .id(employee.getId())
                .userName(employee.getUsername())
                .name(employee.getName())
                .token(token)
                .build();
        return Result.success("员工登录账号",employeeLoginVO);
    }

    @PostMapping("/logout")
    public Result<String> logout() {
        log.info("员工退出账号");
        return Result.success("员工退出账号",null);
    }

    @PostMapping
    public Result insert(@RequestBody EmployeeDTO employeeDTO){
        log.info("新增员工");
        employeeService.insert(employeeDTO);
        return Result.success("新增员工",null);
    }

    @GetMapping("/page")
    public Result<PageResult> getList(String name,
                          @RequestParam(defaultValue = "1") Integer page,
                          @RequestParam(defaultValue = "10") Integer pageSize){
        log.info("分页查询员工");
        PageResult pageResult = employeeService.getList(name, page, pageSize);
        return Result.success("分页查询员工",pageResult);
    }

    @PostMapping("/status/{status}")
    public Result updateStatus(@PathVariable Integer status,Long id){
        log.info("启用禁用员工账号");
        employeeService.updateStatus(status,id);
        return Result.success("启用禁用员工账号",null);
    }

    @PutMapping
    public Result updateUserInfo(@RequestBody Employee employee){
        log.info("编辑员工信息");
        employeeService.updateUserInfo(employee);
        return Result.success("编辑员工信息",null);
    }

    @GetMapping("/{id}")
    public Result<Employee> getInfoById(@PathVariable Integer id){
        log.info("根据员工id查询");
        Employee employee = employeeService.getInfoById(id);
        return Result.success("根据员工id查询",employee);
    }

    @PutMapping("/editPassword")
    public Result modifyPassword(@RequestBody PasswordEditDTO passwordEditDTO) {
        log.info("修改密码");
        employeeService.editPassword(passwordEditDTO);
        return Result.success("修改密码",null);
    }
}
